home *** CD-ROM | disk | FTP | other *** search
- Path: coranto.ucs.mun.ca!usenet
- From: saustin@terra.nlnet.nf.ca (Steve Austin)
- Newsgroups: comp.lang.c++
- Subject: Re: A question of (lexical) style
- Date: Wed, 13 Mar 1996 04:20:26 GMT
- Organization: Kickham Productions
- Message-ID: <4i5ie1$att@coranto.ucs.mun.ca>
- References: <313c99bc.0@193.89.47.9>
- NNTP-Posting-Host: n104h129.nlnet.nf.ca
- X-Newsreader: Forte Agent .99b.112
-
- Brian Elgaard <elgaard@dk-online.dk> wrote:
-
- >A question of (lexical) style
- >
- >I am currently writing guidelines for C++ programmers, and I am in doubt about how to place * and & in declarations.
-
- I've commented on this elsewhere, but I feel this is the source of
- much of the confusion that newcomers to C/C++ feel when presented with
- pointers and references. Textbooks often describe the declaration
-
- 1. int *ip;
-
- as declaring ip as a pointer to an int. This leaves some people with
- the erroneous impression that *ip is a pointer to an int, so that they
- become confused about the use of the * operator. Placing the *
- modifier next to the type being modified seems more logical, and
- avoids this confusion.
-
- Now, K&R read (1) above as declaring *ip to be an integer (Chapter 5,
- p90). Although *ip is obviously an int, looking at the declaration
- this way can lead the unwary into thinking that
-
- 2. int *ip = i; /* where i is an int */
-
- is legal C. After all *ip is an int isn't it? Trouble is, (1) isn't
- declaring the int *ip, it's declaring a pointer, ip.
-
- This potential for confusion is exacerbated for those new to C++ when
- they see
-
- 3. int &ir = i;
-
- Now, &ir is *not* an integer. Perhaps that's why Stroustrup places *
- and & next to the type rather than the object.
-
- Maybe it's the multiple declaration construct which causes the trouble
- here. When you think about it, the ability to declare objects of
- different types on the same line is rather weird. I think it's come to
- seem natural through familiarity, but I do think it adds to a
- beginners confusion surrounding pointers.
-
- Go on, flame me....
-
- Steve
-
-
-
-